# -*- coding: utf-8 -*-
# @Time    : 2018/2/4 15:32
# @Author  : Soft
# @File    : run.py
from flask import Flask, render_template, request
from flask_apscheduler import APScheduler
from playhouse.flask_utils import FlaskDB
from module.models import db
from module.user import user
from module.admin import admin
from module.class_ import clazz
from module.record import record
from module.work import work
from module.z_study import study
from module.location import location
from config import Flask_Config
from module.TimedTask import flush_class_status

app = Flask(__name__)
app.secret_key = 'QD_SYSTEM'
app.config.from_object(Flask_Config())
app.register_blueprint(user)
app.register_blueprint(admin, url_prefix='/admin')
app.register_blueprint(clazz, url_prefix='/class')
app.register_blueprint(record, url_prefix='/record')
app.register_blueprint(work, url_prefix='/work')
app.register_blueprint(study, url_prefix='/study')
app.register_blueprint(location, url_prefix='/location')

FlaskDB(app=app, database=db)  # 自动管理数据库

apsc = APScheduler()
apsc.init_app(app)
apsc.start()

# @app.route("/api")
# def api():
#     return render_template('test/tx1.api.html')
#
# @app.route("/api2")
# def api2():
#     return render_template('test/gd.api.html')
#
# @app.route("/api3")
# def api3():
#     return render_template('test/tx3.api.html')

@app.route("/no")
def _no():
    return render_template('error/nonsupport.html')

@app.errorhandler(404)
def _404(e):
    return render_template('error/404.html', msg=e)


@app.errorhandler(405)
def _405(e):
    return render_template('error/405.html', msg=e)


@app.errorhandler(500)
def _500(e):
    return render_template('error/500.html', msg=e)


flush_class_status()
if __name__ == '__main__':
    # app.debug = True
    app.run(host='0.0.0.0', port=8080, threaded=True, debug=False)
